package com.sharemarking.wa.platform.mapper;

import com.sharemarking.wa.common.entity.OrgVerify;
import com.sharemarking.wa.platform.dto.orderCommon.OrderListDto;


import com.sharemarking.wa.platform.dto.orderCommon.TaskOrderListDto;
import com.sharemarking.wa.platform.dto.orderCommon.WasteListDto;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Repository
public interface OrderCommonMapper {
    @Select("SELECT a.real_weight,a.pics AS weightPicList,b.pic,a.waste_id," +
            "(SELECT waste_type_name FROM wa_org_waste x WHERE x.id = a.waste_id) AS waste_type_name" +
            ",a.NAME AS wasteName,a.weight AS weight,t.receiveWeight,t.realWeight,\n" +
            "\n" +
            "b.name AS wasteName,b.characteristics,b.form,b.package_type,b.dangerous_ingredients,b.measure\n" +
            "\n" +
            "FROM wa_order_details a \n" +
            "LEFT JOIN (\n" +
            "\n" +
            "SELECT waste_id,SUM( real_weight ) AS realWeight,SUM( weight ) AS receiveWeight \n" +
            "FROM wa_order_details \n" +
            "WHERE order_id IN ( SELECT id FROM wa_order WHERE parent_id = #{orderId} AND state != 10) \n" +
            "\n" +
            "GROUP BY waste_id\n" +
            "\n" +
            ")t ON a.waste_id = t.waste_id \n" +
            "LEFT JOIN wa_org_waste b ON a.waste_id = b.id\n" +
            "WHERE a.order_id = #{orderId}")
    List<WasteListDto> getMyOrderWasteListByAllOrder(Integer orderId);


    @Select("SELECT b.pic,a.waste_id,a.name AS wasteName,a.weight,b.weight AS receiveWeight ,b.real_weight FROM wa_order_details a \n" +
            "LEFT JOIN (SELECT waste_id, name,weight,real_weight FROM wa_order_details WHERE order_id = #{orderId} GROUP BY waste_id,name) b ON a.waste_id = b.waste_id \n" +
            "LEFT JOIN wa_org_waste b ON a.waste_id = b.id "+
            "WHERE a.order_id = #{parentId}")
    List<WasteListDto> getMyOrderWasteListByChildOrder(@Param("orderId") Integer orderId, @Param("parentId") Integer parentId);


    List<OrderListDto> getList(Map map);

    Integer getListTotal(Map map);

    @Select("SELECT b.id AS wasteId,b.pic,a.name AS wasteName,a.weight,a.real_weight " +
            "FROM wa_order_details a LEFT JOIN wa_org_waste b ON a.waste_id = b.id WHERE a.order_id = #{orderId}")
    List<WasteListDto> getWasteList(Integer orderId);


//    @Select("SELECT\n" +
//            "a.waste_id,a.NAME AS wasteName,SUM( a.weight ) AS weight,SUM( a.real_weight ) AS realWeight,\n" +
//            "b.weight AS receiveWeight,c.pic,c.waste_type_name,c.characteristics,c.form,c.transfer_purpose,\n" +
//            "c.package_type,c.dangerous_ingredients,c.measure \n" +
//            "FROM\n" +
//            "wa_order_details a\n" +
//            "LEFT JOIN ( SELECT waste_id,name,sum(weight) as weight FROM wa_order_details WHERE order_id in (SELECT id FROM wa_order WHERE parent_id = #{orderId} AND state != 10) ) b ON a.waste_id = b.waste_id LEFT JOIN wa_org_waste c ON c.id = a.waste_id \n" +
//            "WHERE a.order_id = #{orderId} GROUP BY waste_id")
//    //总订单获取废品列表
//    List<WasteListDto> getChildOrderWasteList(@Param("orderId") Integer orderId);

    @Select("SELECT b.pic AS pic,a.name AS wasteName,a.waste_id,a.pics AS weightPicList,a.weight,a.real_weight,b.waste_type_name,b.characteristics,b.form,b.transfer_purpose,b.package_type,b.dangerous_ingredients,b.measure " +
            "FROM wa_order_details a LEFT JOIN wa_org_waste b ON b.id = a.waste_id WHERE a.order_id= #{orderId}")
    List<WasteListDto> getChildOrderWasteListByOrderId(Integer orderId);


    List<TaskOrderListDto> getTaskOrderList(Map map);

    Integer getTaskOrderListTotal(Map map);

    List<Integer> getManagementTaskList(Map map);

    @Select("SELECT a.code,a.is_urgent,IFNULL(b.real_name,b.name) AS publishOrgName,arrival_time FROM wa_order a " +
            "LEFT JOIN sys_org b ON a.send_org_id = b.id " +
            "WHERE a.id = #{orderId} ")
    TaskOrderListDto getOrderInfoById(Integer orderId);


    List<WasteListDto> getReceiveOrderWasteList(@Param("orderId") Integer orderId,@Param("orgId")Integer orgId,@Param("pageIndex")Integer pageIndex,@Param("pageSize")Integer pageSize,@Param("searchStr") Object searchStr);

    Integer getReceiveOrderWasteListPageTotal(@Param("orderId") Integer orderId,@Param("orgId")Integer orgId);

    //这句比较特殊，写在xml文件中
    Integer getStateValueBySendOrgMemberId(@Param("SendOrgMemberId") Integer SendOrgMemberId,@Param("state") Integer state);

    //这句比较特殊，写在xml文件中
    Integer getStateValueByReceiveOrgMemberId(@Param("ReceiveOrgMemberId") Integer ReceiveOrgMemberId,@Param("state") Integer state);

    @Select("SELECT COUNT(*) FROM wa_order WHERE transport_org_member_id = #{transportOrgMemberId} AND state = #{state}")
    Integer getStateValueByTransportOrgMemberId(@Param("transportOrgMemberId") Integer transportOrgMemberId,@Param("state") Integer state);

    @Select("SELECT COUNT(*) FROM wa_order WHERE driver_id = #{driverId} AND state = #{state}")
    Integer getStateValueByDriverId(@Param("driverId") Integer driverId,@Param("state") Integer state);

    @Select("SELECT COUNT(*) FROM wa_order WHERE transport_org_id = #{transportOrgId} AND state = #{state}")
    Integer getStateValueByTransportOrgId(@Param("transportOrgId") Integer driverId,@Param("state") Integer state);

    @Select("SELECT a.waste_id,a.name AS wasteName,a.weight,b.pic,b.waste_type_name,b.characteristics,b.form,b.transfer_purpose,b.package_type,b.dangerous_ingredients,b.measure " +
            "FROM wa_order_details a LEFT JOIN wa_org_waste b ON a.waste_id = b.id " +
            "WHERE a.order_id = #{id} AND a.waste_id NOT IN " +
            "(SELECT a.waste_id FROM wa_order_details a LEFT JOIN wa_order b ON a.order_id = b.id WHERE b.parent_id = #{id} AND b.state != 9 ) ")
    List<WasteListDto> getReceiveOrderWasteListInfo(Integer id);

    @Select("SELECT exam_state FROM wa_org_verify a LEFT JOIN sys_org_member b ON a.org_id = b.org_id " +
            "WHERE b.member_id = #{memberId}")
    Integer isOrgVerify(Integer memberId);
}
